﻿using System;
using Studio;
using UnityEngine;

namespace WzhAddon
{
	public class ObjectScreenController2 : MonoBehaviour
	{
		public Transform target;
		private Vector3 screenPoint;
		private Vector3 offset;
		private CameraControl camCtrl;
		private bool isTracking;
		private Camera mainCamera;
		public Color selectedColor = new Color(0f, 0f, 1f, 0.3f);
		public Color normalColor = new Color(0f, 1f, 0f, 0.3f);
		private void Awake()
		{
			if (this.target == null)
			{
				this.target = base.transform;
			}
		}
		private void Start()
		{
			if (this.camCtrl == null)
			{
				if (null != Camera.main)
				{
					this.mainCamera = Camera.main;
					this.camCtrl = Camera.main.GetComponent<CameraControl>();
				}
				else
				{
					GameObject gameObject = GameObject.FindGameObjectWithTag("MainCamera");
					if (gameObject)
					{
						this.mainCamera = gameObject.GetComponent<Camera>();
						this.camCtrl = gameObject.GetComponent<CameraControl>();
					}
				}
			}
			if (base.GetComponent<Collider>() != null)
			{
				base.GetComponent<Collider>().enabled = false;
			}
		}
		private void FixedUpdate()
		{
			if (Input.GetMouseButtonDown(0))
			{
				base.GetComponent<Collider>().enabled = true;
			}
		}
		private void Update()
		{
			if (Input.GetMouseButtonDown(0))
			{
				base.GetComponent<Collider>().enabled = true;
			}
		}
		private void OnMouseDown()
		{
			this.screenPoint = this.mainCamera.WorldToScreenPoint(this.target.position);
			this.offset = this.target.position - this.mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, this.screenPoint.z));
			Singleton<GameCursor>.Instance.SetCursorLock(false);
			base.GetComponent<Collider>().enabled = false;
			this.isTracking = true;
			if (base.GetComponent<Renderer>())
			{
				base.GetComponent<Renderer>().material.color = this.selectedColor;
			}
		}
		private void OnMouseDrag()
		{
			Vector3 vector = new Vector3(Input.mousePosition.x, Input.mousePosition.y, this.screenPoint.z);
			Vector3 position = this.mainCamera.ScreenToWorldPoint(vector) + this.offset;
			this.target.position = position;
		}

		private void OnMouseUp()
		{
			if (base.GetComponent<Renderer>())
			{
				base.GetComponent<Renderer>().material.color = this.normalColor;
			}
			base.GetComponent<Collider>().enabled = false;
		}

	
	}
}
